package common

import (
	"os"
	"path"
	"runtime"
	"time"

	zaprotatelogs "github.com/lestrrat-go/file-rotatelogs"
	"go.uber.org/zap/zapcore"
)

//@function: GetWriteSyncer
//@description: zap logger中加入file-rotatelogs
//@return: zapcore.WriteSyncer, error

func GetWriteSyncer() (zapcore.WriteSyncer, error) {
	sysType := runtime.GOOS

	switch sysType {
	case "windows":
		fileWriter, err := zaprotatelogs.New(
			path.Join(CONFIG.Zap.Director, "%Y-%m-%d.log"),
			zaprotatelogs.WithMaxAge(7*24*time.Hour),
			zaprotatelogs.WithRotationTime(24*time.Hour),
		)
		if CONFIG.Zap.LogInConsole {
			return zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(fileWriter)), err
		}
		return zapcore.AddSync(fileWriter), err
	default:
		fileWriter, err := zaprotatelogs.New(
			path.Join(CONFIG.Zap.Director, "%Y-%m-%d.log"),
			zaprotatelogs.WithLinkName(CONFIG.Zap.LinkName),
			zaprotatelogs.WithMaxAge(7*24*time.Hour),
			zaprotatelogs.WithRotationTime(24*time.Hour),
		)
		if CONFIG.Zap.LogInConsole {
			return zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(fileWriter)), err
		}
		return zapcore.AddSync(fileWriter), err

	}
}
